package xiangya.bysj.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import xiangya.bysj.annotation.WebValidate;
import xiangya.bysj.entity.Admin;
import xiangya.bysj.entity.Doctor;
import xiangya.bysj.entity.Patient;
import xiangya.bysj.entity.User;
import xiangya.bysj.service.impl.AdminServiceImpl;
import xiangya.bysj.service.impl.DoctorServiceImpl;
import xiangya.bysj.service.impl.HistoryTakeServiceImpl;
import xiangya.bysj.service.impl.PatientServiceImpl;
import xiangya.bysj.service.impl.TakeInfoServiceImpl;
import xiangya.bysj.service.impl.UserServiceImpl;

@Controller
@RequestMapping("admin")
public class AdminController {

	@Resource
	HistoryTakeServiceImpl hisTakeService;
	@Resource(name = "adminService")
	private AdminServiceImpl adminService;
	@Resource
	DoctorServiceImpl doctorService;
	@Resource
	UserServiceImpl userService;
	@Resource
	PatientServiceImpl patientService;
	


	@RequestMapping( "login")
	@WebValidate(isValidate = false)
	public String login(HttpServletRequest req ,String username, String password) {
		try {
			
			HttpSession session = req.getSession();
			Admin admin = adminService.login(username, password);
			if(!(null==admin))
			{
				session.setAttribute("ADMIN", admin);
			}
			else
			{
				return "web/admin_login";
			}

		} catch (Exception e) {
			e.printStackTrace();
			return "web/admin_login";

		}
		return "web/index";
	}
	
	@RequestMapping("logout")
	public String logout(HttpServletRequest req) {
		try {
			
			
			HttpSession session = req.getSession();
			session.removeAttribute("ADMIN");
			
			
		} catch (Exception e) {

		}
		return "web/admin_login";
	}

	@RequestMapping("index")
	public String index()
	{
		return "web/admin_login";
	}
	
	@RequestMapping("verify")
	public String verify()
	{
		return "web/verify";
	}
	
	@RequestMapping("getUncheckedDoctor")
	@ResponseBody
	public Object getUncheckedDoctor(@RequestParam("offset") int offset)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			int uncheckedDoctorCount = 0;
			uncheckedDoctorCount = doctorService.selectUncheckedDoctorCount();
			List<Doctor> lt = doctorService.selectUncheckedDoctor(offset);
			if(lt == null){
				lt = new ArrayList<Doctor>();
			
			}
			map.put("rows",lt );
			map.put("total",uncheckedDoctorCount);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
		
	}
	@RequestMapping("action/accept")
	@ResponseBody
	public Object accpet(@RequestParam("ids[]") int[] ids)
	{
		Map<String,Object> map = new HashMap<String,Object>();

		try {
			for(int i : ids)
				
			doctorService.updateDoctorStatus(i , 1);
			
			map.put("status","ok");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
		
	}
	@RequestMapping("action/noaccept")
	@ResponseBody
	public Object noaccpet(@RequestParam("ids[]") int[] ids)
	{
		Map<String,Object> map = new HashMap<String,Object>();

		try {
			for(int i: ids)
			doctorService.updateDoctorStatus( i, 2);

			map.put("status","ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	@RequestMapping("see")
	public String see(Model model)
	{

		List<User> users = userService.selectUser();
		model.addAttribute("users",users);
		return "web/see";
	}
	
	@RequestMapping("getAllPatient")
	@ResponseBody
	public Object getAllPatient(int userid){
		Map<String,Object> obj = new HashMap<String,Object>();
		
		try {
			obj.put("patients",patientService.findPatientsByUser(userid));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obj;
	}
	@RequestMapping("getTakeInfo")
	@ResponseBody
	public Object getTakeInfo(int patientid,int offset){
		Map<String,Object> obj = new HashMap<String,Object>();
		
		try {
			List<Patient> list=new ArrayList<Patient>();
			Patient p = new Patient();
			p.setId(patientid);
			list.add(p);
			obj.put("rows",hisTakeService.getByPatientId(offset, patientid));
			obj.put("total",hisTakeService.getHistCountByPatient(patientid));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obj;
	}
	
}
